"use strict";

function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.ActionSheetMiniLayout = void 0;

var _react = _interopRequireWildcard(require("react"));

var _classnames = _interopRequireDefault(require("classnames"));

var _configContext = require("../../../extends/configContext");

var _components = require("@tarojs/components");

var _index = _interopRequireDefault(require("../../popup/index"));

var _index2 = _interopRequireDefault(require("../../spin/index"));

var _index3 = _interopRequireDefault(require("../../icon/index"));

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }

function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }

function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) { symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); } keys.push.apply(keys, symbols); } return keys; }

function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }

function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }

var ActionSheetMiniLayout = function ActionSheetMiniLayout(_ref) {
  var _classNames2;

  var _ref$visible = _ref.visible,
      visible = _ref$visible === void 0 ? false : _ref$visible,
      _ref$actions = _ref.actions,
      actions = _ref$actions === void 0 ? [] : _ref$actions,
      title = _ref.title,
      _ref$zIndex = _ref.zIndex,
      zIndex = _ref$zIndex === void 0 ? 100 : _ref$zIndex,
      _ref$cancelText = _ref.cancelText,
      cancelText = _ref$cancelText === void 0 ? '取消' : _ref$cancelText,
      _ref$cancelButton = _ref.cancelButton,
      cancelButton = _ref$cancelButton === void 0 ? true : _ref$cancelButton,
      _ref$closeOnClickActi = _ref.closeOnClickAction,
      closeOnClickAction = _ref$closeOnClickActi === void 0 ? true : _ref$closeOnClickActi,
      _ref$closeOnClickOver = _ref.closeOnClickOverlay,
      closeOnClickOverlay = _ref$closeOnClickOver === void 0 ? true : _ref$closeOnClickOver,
      _ref$layout = _ref.layout,
      layout = _ref$layout === void 0 ? 'column' : _ref$layout,
      _ref$number = _ref.number,
      number = _ref$number === void 0 ? 5 : _ref$number,
      onClose = _ref.onClose,
      onCancel = _ref.onCancel,
      _ref$onSelect = _ref.onSelect,
      onSelect = _ref$onSelect === void 0 ? function () {} : _ref$onSelect,
      className = _ref.className,
      style = _ref.style,
      buttonStyle = _ref.buttonStyle,
      buttonClassName = _ref.buttonClassName;
  (0, _react.useEffect)(function () {
    if (layout === 'row' && !actions[0].icon && !actions[0].description || layout === 'column' && !actions[0].content) {
      console.warn('[Discuz-design WARN]:' + '检查参数layout与actions是否匹配');
    }
  });

  var _useConfig = (0, _configContext.useConfig)(),
      clsPrefix = _useConfig.clsPrefix;

  var handleClick = function handleClick(e, item) {
    if (typeof onSelect === 'function') {
      onSelect(e, item);
    }

    if (typeof onClose === 'function' && closeOnClickAction) {
      onClose();
    }
  };

  var handleCancle = function handleCancle() {
    if (typeof onCancel === 'function') {
      onCancel();
    }

    if (typeof onClose === 'function') {
      onClose();
    }
  }; // 布局方式为column时渲染的item


  var renderColumn = function renderColumn(item, index) {
    return /*#__PURE__*/_react.default.createElement(_components.View, {
      key: index,
      className: (0, _classnames.default)("".concat(clsPrefix, "-action-sheet__item"), item.className),
      style: item.color ? _objectSpread(_objectSpread({}, item.style), {}, {
        color: "".concat(item.color)
      }) : item.style,
      onClick: function onClick(e) {
        if (item.disabled) {
          e.stopPropagation();
          return;
        }

        ;
        handleClick(e, item);
      }
    }, item.loading ? /*#__PURE__*/_react.default.createElement(_index2.default, {
      size: "16px",
      type: 'spinner'
    }) : item.content);
  }; // 布局方式为row时渲染的item


  var renderRow = function renderRow(item, index) {
    return /*#__PURE__*/_react.default.createElement(_components.View, {
      className: (0, _classnames.default)(item.className, "".concat(clsPrefix, "-action-sheet__moreItem"), _defineProperty({}, "".concat(clsPrefix, "-action-sheet__flex-more-item"), actions.length < number)),
      onClick: function onClick(e) {
        handleClick(e, item);
      },
      key: index,
      style: item.style
    }, !item.canShare ? /*#__PURE__*/_react.default.createElement(_components.View, {
      className: (0, _classnames.default)("".concat(clsPrefix, "-action-sheet__icon"))
    }, /*#__PURE__*/_react.default.createElement(_index3.default, {
      name: item.icon,
      size: 26
    })) : /*#__PURE__*/_react.default.createElement(_components.Button, {
      className: (0, _classnames.default)("".concat(clsPrefix, "-action-sheet__icon"), "".concat(clsPrefix, "-action-sheet__button")),
      openType: "share",
      "data-shareData": item.shareData
    }, /*#__PURE__*/_react.default.createElement(_index3.default, {
      name: item.icon
    })), /*#__PURE__*/_react.default.createElement(_components.View, {
      className: (0, _classnames.default)("".concat(clsPrefix, "-action-sheet__text"))
    }, item.description));
  }; // 渲染item


  var renderItem = function renderItem(item, index) {
    // 自定义节点
    if ( /*#__PURE__*/_react.default.isValidElement(item.element)) {
      return /*#__PURE__*/_react.default.createElement(_components.View, {
        key: index
      }, item.element);
    } // 布局方式为column时
    else if (layout === 'column') {
      return renderColumn(item, index);
    } else {
      return renderRow(item, index);
    }
  }; // 布局为grid时，根据传入的number判断一行需要几列


  var gridMoreItem = {
    gridTemplateColumns: "repeat(".concat(number, ", 40px)")
  }; // 为Popup设置层级

  var popupStyle = {
    zIndex: zIndex
  };
  return /*#__PURE__*/_react.default.createElement(_index.default, {
    visible: visible,
    onClose: onClose,
    position: "bottom",
    maskClosable: closeOnClickOverlay,
    style: popupStyle
  }, /*#__PURE__*/_react.default.createElement(_components.View, {
    className: (0, _classnames.default)("".concat(clsPrefix, "-action-sheet"), className),
    style: style
  }, title && /*#__PURE__*/_react.default.createElement(_components.View, {
    className: (0, _classnames.default)("".concat(clsPrefix, "-action-sheet__title"))
  }, title), actions && /*#__PURE__*/_react.default.createElement(_components.View, {
    className: (0, _classnames.default)("".concat(clsPrefix, "-action-sheet__content"), (_classNames2 = {}, _defineProperty(_classNames2, "".concat(clsPrefix, "-action-sheet__row"), layout === 'row'), _defineProperty(_classNames2, "".concat(clsPrefix, "-action-sheet__grid"), layout === 'row' && actions.length >= number), _defineProperty(_classNames2, "".concat(clsPrefix, "-action-sheet__flex"), layout === 'row' && actions.length < number), _classNames2)) // 根据传入的number，动态计算grid的列数
    ,
    style: actions.length >= number ? gridMoreItem : {}
  }, actions.map(function (item, index) {
    return renderItem(item, index);
  })), cancelButton && /*#__PURE__*/_react.default.createElement(_components.View, {
    className: (0, _classnames.default)(buttonClassName, "".concat(clsPrefix, "-action-sheet__bottom")),
    onClick: handleCancle,
    style: buttonStyle
  }, cancelText)));
};

exports.ActionSheetMiniLayout = ActionSheetMiniLayout;